#!/bin/sh

# Slackware build script for guitarix
# Originally written by Michales Michaloudes (email removed)

# Modified by B. Watson <yalhcru@gmail.com>. Modified version released
# under the WTFPL, for details see http://www.wtfpl.net/txt/copying/

# 20170825 bkw:
# - Updated for 0.35.6
# - Install the bundled roboto font.

# 20170725 bkw:
# - Updated for 0.35.5

# 20170710 bkw:
# - Updated for 0.35.4
# - Remove README stuff about webkitgtk, since upstream dropped it.
# - Add README.online with troubleshooting info. I had trouble
#   getting the new Online Presets stuff to work, so hopefully this
#   might help the next guy.

# 20170404 bkw:
# - Updated for 0.35.3
# - Use + rather than ; in find/chmod. Noticeably faster.

# 20170306 bkw:
# - Flip changelog comments right-side up
# - Stop writing outside of $TMP.

# 20161016 bkw:
# - Updated for 0.35.2

# 20160806 bkw:
# - Updated for 0.35.1
# - removed WEBKIT=no option from README (it was removed from the
#   script in March, should have been removed from README then).
# - removed webkitgtk from REQUIRES, as it's now optional (and
#   autodetected, no way to force-disable it).
# - removed patches, since they're already included in the new release.
# - remove unimplemented SSE=no option. Note that SSE support is still
#   autodetected, so if someone's trying this on e.g. ARCH=arm, it should
#   automatically build without SSE (I have no idea if the build will
#   actually complete, in that case).
# - remove .desktop sed stuff, upstream fixed it.
# - update man page.
# - note: zita-convolver and zita-resampler are not requirements. they
#   will be linked if installed, otherwise guitarix's bundled copies will
#   be used. I only mention this here so I won't forget again...

# 20160329 dbs:
# - Updated for Slackware-14.2.  Forced -msse in SLKCFLAGS.
#   This just won't build without SSE on 32 bit gcc-5.3.0 :(
#     xmmintrin.h:857:1: error: inlining failed in call to always_inline
#     ‘void _MM_SET_EXCEPTION_STATE(unsigned int)’: target specific option mismatch
#   Patch 0003-fix-build-even-for-gcc4.8.patch doesn't fix it, and there
#   are no further fixes upstream at this time.

# 20151108 bkw:
# - Updated for 0.34.0
# - Added --disable-cxx11 to ./waf flags, as bluetooth.h uses the gcc
#   typeof() extension. Not sure if newer versions of bluez would have
#   this issue (but Slackware 14.1's bluez is pretty old by now).
# - Added patch from upstream git to allow building without webkitgtk.
#   The author added support for downloading presets, but didn't make
#   it optional in 0.34.0. Also added a couple of bugfix patches.
# - Support building 0.33.0 with VERSION=0.33.0 in the env.
# - Add SSE=no option.
# - Add WEBKIT=no option.
# - Remove LV2=no option from README (but not from script).

# 20150514 bkw:
# - Updated for 0.32.3
# - Fix some issues with the shared libs: they were being installed
#   to /usr/lib/ on 64-bit, and they were missing the +x bits.
# - Made LV2 option default to yes, since upstream is requiring lilv
#   even if --no-lv2 is used. Also, the lv2 plugins were being
#   installed to /usr/lib/ on 64-bit. Grr.
# - Upstream wants avahi by default. For now, hardcoded --no-avahi.

# 20130430 bkw:
# - Updated for 0.27.1
# - Replaced slack-desc with updated text from guitarix site
# - Added LV2 option

# 20121216 bkw:
# - Upgraded to guitarix2
# - Added man page
# - Added realtime capabilities
# - Make .desktop file validate
# - Add guitarix's README to /usr/doc

PRGNAM=guitarix
VERSION=${VERSION:-0.35.6}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686 -msse"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686 -msse"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

# Tarballs are now named "guitarix2-$VERSION", but the dir inside the
# tarball is still "guitarix-$VERSION".
TARNAME=${PRGNAM}2

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$TARNAME-$VERSION.tar.xz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \+ -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \+

# guitarix wants to install the roboto font in the wrong place for
# slackware.
sed -i '/fonts/s,\<truetype\>,TTF,' wscript tools/undiag

# this is no longer documented in README, but still here for debugging.
if [ "${LV2:-yes}" = "no" ]; then
  LV2OPT=--no-lv2
fi

# grrr, -DNDEBUG doesn't get added to the flags automatically...
LIBDIR="/usr/lib${LIBDIRSUFFIX}"
./waf configure \
  --prefix=/usr \
  --cxxflags-release="$SLKCFLAGS -DNDEBUG" \
  --ladspadir="$LIBDIR/ladspa/" \
  --lv2dir="$LIBDIR/lv2/" \
  --libdir="$LIBDIR" \
  --no-avahi \
  --no-ldconfig \
  --no-desktop-update \
  $EXTRAOPT \
  $LV2OPT
./waf build # -v # verbose, shows actual commands
./waf install --destdir=$PKG

# --install-roboto-font does the wrong thing, we'll install it manually.
mkdir -p $PKG/usr/share/fonts/TTF
install -m0644 fonts/* $PKG/usr/share/fonts/TTF

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

# Shared libraries are supposed to be +x.
chmod 755 $PKG/$LIBDIR/libgx*.so.?.?

# Man page written for this SlackBuild, see guitarix.rst.
mkdir -p $PKG/usr/man/man1
gzip -9c < $CWD/$PRGNAM.1 > $PKG/usr/man/man1/$PRGNAM.1.gz

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a COPYING changelog README* $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
find $PKG/usr/doc -type f -exec chmod 0644 {} \;

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh  > $PKG/install/doinst.sh

# Only add capability stuff if not disabled:
if [ "${SETCAP:-yes}" = "yes" ]; then
  cat $CWD/setcap.sh >> $PKG/install/doinst.sh
  # Only allow execution by audio group
  chown root:audio $PKG/usr/bin/$PRGNAM
  chmod 0750 $PKG/usr/bin/$PRGNAM
fi

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
